package com.bjpowernode.boss.mvc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjpowernode.boss.PowerBossApplication;
import com.bjpowernode.boss.common.enums.PublicStatus;
import com.bjpowernode.boss.common.enums.ResponseCode;
import com.bjpowernode.boss.common.exception.BossException;
import com.bjpowernode.boss.common.utils.NoUtil;
import com.bjpowernode.boss.dao.entity.PayWay;
import com.bjpowernode.boss.dao.mapper.PayWayMapper;
import com.bjpowernode.boss.dao.result.DicEntity;
import com.bjpowernode.boss.mvc.models.form.payway.ConfigProductWayForm;
import com.bjpowernode.boss.mvc.service.PayWayService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PayWayServiceImpl implements PayWayService {

    @Resource
    private PayWayMapper payWayMapper;

    @Override
    public IPage<PayWay> pageList(Integer pageNo, Integer pageSize, String productCode) {

        QueryWrapper<PayWay> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("pay_product_code","way_code","type_code");
        if(StrUtil.isNotBlank(productCode)){
            wrapper.eq("pay_product_code",productCode.trim());
        }
        return payWayMapper.selectPage(Page.of(pageNo,pageSize),wrapper);
    }

    @Override
    public List<DicEntity> getPayWayDicListFromCache() {
        return PowerBossApplication.PAYWAY_DIC_LIST;
    }

    @Override
    public List<DicEntity> getPayTypeDicListFromCache(String payWayCode) {
        List<DicEntity> dicEntityList = PowerBossApplication.PAYTYPE_DIC_MAP.get(payWayCode);
        return dicEntityList ==  null ? new ArrayList<>( ) : dicEntityList;
    }

    @Override
    public void configProductPayWay(ConfigProductWayForm configPayWayForm) {
        // 1. product_code, pay_code ,type_code 三者是唯一的
        QueryWrapper<PayWay> wrapper = new QueryWrapper<>();
        wrapper.eq("pay_product_code",configPayWayForm.getProductCode())
                .eq("way_code",configPayWayForm.getWayCode())
                .eq("type_code",configPayWayForm.getTypeCode());

        PayWay payWay = payWayMapper.selectOne(wrapper);

        if(payWay != null){
            throw new BossException(ResponseCode.PAYMENT_PRODUCT_CONFIG_EXISTS);
        }

        //2.添加支付方式配置
        String wayCode = configPayWayForm.getWayCode();
        String typeCode = configPayWayForm.getTypeCode();

        payWay  = new PayWay();
        payWay.setId(NoUtil.getUUID());
        payWay.setWayCode(wayCode);
        payWay.setWayName(PowerBossApplication.PAYWAY_NAME_MAP.get(wayCode));
        payWay.setTypeCode(typeCode);
        payWay.setTypeName(PowerBossApplication.PAYTYPE_NAME_MAP.get( wayCode+":"+typeCode));
        payWay.setPayProductCode(configPayWayForm.getProductCode());
        payWay.setPayRate(configPayWayForm.getPayRate());
        payWay.setSorts(configPayWayForm.getSorts());
        payWay.setStatus(PublicStatus.ENABLE.name());
        payWay.setEditTime(new Date());
        payWay.setRemark("配置支付产品的支付方式");
        payWayMapper.insert(payWay);
    }
}
